
import hibernate.p.DocType;
import java.util.*;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.*;
import org.zkoss.zul.*;
import utilities.utilities_components.*;
import utilities.event.*;

public class CheckInController
extends Div{
	
	private PlainList list;
	private String[] head={"ID","客户姓名","房号","房型","钟房","单价","预计离开时间","预计价格"};
	
	private Textbox name;
	public void nameValidate(){
		if(name.getText() == null || name.getText().equals(""))
			throw new WrongValueException(name,"名字不能为空");
		if(name.getText().length() > 10)
			throw new WrongValueException(name,"名字不能超过10个字符");
		this.getPage().setAttribute("NAME",name.getText());
	}
	
	private Longbox telPhone;
	public void telPhoneValidate(){
		if(telPhone.getValue() == null || telPhone.getValue() <= 0)
			throw new WrongValueException(telPhone,"请检查电话号码格式");
		this.getPage().setAttribute("TELNUMBER",telPhone.getValue());
	}
	
	private Listbox doctype;
	public void doctypeValidate(){
		if(doctype.getSelectedIndex() == 0)
			this.getPage().setAttribute("DOCTYPE",DocType.get("ID_CARD"));
		else if(doctype.getSelectedIndex() == 1)
			this.getPage().setAttribute("DOCTYPE",DocType.get("STUDENT_CARD"));
		else if(doctype.getSelectedIndex() == 2)
			this.getPage().setAttribute("DOCTYPE",DocType.get("MILITANT_CARD"));
		else throw new WrongValueException(doctype,"请选择证件类型");
	}
	
	private Textbox docNumber;
	public void docNumberValidate(){
		if(docNumber.getText() == null || docNumber.getText().equals(""))
			throw new WrongValueException(docNumber,"请输入证件号码");
		if(docNumber.getText().length() > 20)
			throw new WrongValueException(docNumber,"证件号码不会大于20位");
		else if(doctype.getSelectedIndex() == 0)
			for(int i=0;i < docNumber.getText().length();i++)
				if(docNumber.getText().charAt(i) < '0' || docNumber.getText().charAt(i) > '9')
					throw new WrongValueException(docNumber,"身份证必须全部为数字");
		this.getPage().setAttribute("DOCNUMBER",docNumber.getText());
	}
	
	private Datebox birthday;
	public void birthdayValidate(){
		java.util.Calendar c=java.util.Calendar.getInstance();
		c.set(java.util.Calendar.YEAR,1900);
		c.set(java.util.Calendar.MONTH,0);
		Date date1=c.getTime();
		Date date2=new Date();
		if(birthday.getValue() == null){
			this.getPage().setAttribute("BIRTHDAY",null);
			return;
		}
	
		if(birthday.getValue().compareTo(date1) < 0 || birthday.getValue().compareTo(date2) > 0)
			throw new WrongValueException(birthday,"生日时间超出范围");
		this.getPage().setAttribute("BIRTHDAY",birthday.getValue());
	}
	
	
	private Listbox sex;
	public void sexValidate(){
		if(sex.getSelectedIndex() == 0)
			this.getPage().setAttribute("SEX",true);
		else if(sex.getSelectedIndex() == 1)
			this.getPage().setAttribute("SEX",false);
		else throw new WrongValueException(sex,"请选择性别");
	}
	
	private Textbox region,province,address;
	public void regionValidate(){
		if(region.getText() != null && region.getText().length() > 20)
			throw new WrongValueException(region,"区域不能多于20字符");
		this.getPage().setAttribute("REGION",region.getText());
	}
	
	public void provinceValidate(){
		if(province.getText() != null && province.getText().length() > 40)
			throw new WrongValueException(province,"省份不能多于40字符");
		this.getPage().setAttribute("PROVINCE",province.getText());
	}
	
	public void addressValidate(){
		if(address.getText() == null || address.getText().equals(""))
			throw new WrongValueException(address,"区域不能为空");
		if(address.getText() != null && address.getText().length() > 200)
			throw new WrongValueException(address,"区域不能多于200字符");
		this.getPage().setAttribute("ADDRESS",address.getText());
	}
	
	private Textbox note;
	public void noteValidate(){
		if(note.getText() != null && note.getText().length() > 200)
			throw new WrongValueException(note,"备注长度不超过200个字符");
		this.getPage().setAttribute("NOTE",note.getText());
	}
	
	private Checkbox isVip;
	private Button add,change,delete,count;
	private Checkbox receiveDeposite;
	private Listbox select;
	private Listbox roomType;
	
	private Intbox roomNumber;
	public void roomNumberValidate(){
		Integer number=roomNumber.getValue();
		if(number == null)
			throw new WrongValueException(roomNumber,"请输入房间数量");
		if(number <= 0)
			throw new WrongValueException(roomNumber,"房间数量必须大于0间");
		if(number >= 99)
			throw new WrongValueException(roomNumber,"房间数量必须小于99间");
		this.getPage().setAttribute("ROOM_NUMBER",number);
	}

	private Listbox isPart;
	
	private Datebox timeLeaving;
	public void timeLeavingValidate(){
		Date now=new Date();
		if(timeLeaving.getValue() == null)
			throw new WrongValueException(timeLeaving,"请输入预计离开时间");
		if(timeLeaving.getValue().compareTo(now) < 0)
			throw new WrongValueException(timeLeaving,"时间不能早于当前");
		this.getPage().setAttribute("TIME_LEAVING",timeLeaving.getValue());
	}
	
	private Doublebox deposite;
	public void depositeValidate(){
		this.getPage().setAttribute("DEPOSITE",null);
		Double value=deposite.getValue();
		if(value == null && receiveDeposite.isChecked())
			throw new WrongValueException(deposite,"请输入定金");
		if(value != null && value < 0 && receiveDeposite.isChecked())
			throw new WrongValueException(deposite,"定金不能为负");
		this.getPage().setAttribute("DEPOSITE",value);
	}
	
	private Textbox payNote;
	public void payNoteValidate(){
		if(payNote.getText() != null && payNote.getText().length() > 200)
			throw new WrongValueException(payNote,"备注长度不超过200个字符");
		this.getPage().setAttribute("PAYNOTE",payNote);
	}
	
	private Textbox billNumber;
	public void billNumberValidate(){
		if(billNumber.getText() == null || billNumber.getText().equals(""))
			throw new WrongValueException(billNumber,"请输入帐单号");
		if(billNumber.getText().length() > 50)
			throw new WrongValueException(billNumber,"帐单号必须小于50位");
		for(int i=0;i < billNumber.getText().length();i++){
			char ith=billNumber.getText().charAt(i);
			if(!(ith >= 'a' && i <= 'z') &&
			   !(ith >= '1' && ith <= '9') &&
			   !(ith >= 'A' && ith <= 'Z') && 
			   ith != '-')
				throw new WrongValueException(billNumber,"账单号只能是a-z或A-Z或1-9或符号'-'");
		}
		this.getPage().setAttribute("BILLNUMBER",billNumber.getText());
	}
	
	public CheckInController(){
		list=new PlainList();
		list.setHead(head);
		list.setParent(this);
		list.setListener(new PlainList.SelectListener(){
			public void onFocus(Component comp){
				int col=list.getSelectedIndex();
				if(col >= 0){
					list.getPage().setAttribute("SELCOL",list.getColDate(col));
					delete.setDisabled(false);
				}else{
					list.getPage().setAttribute("SELCOL",null);
					delete.setDisabled(true);
				}
			}
		});
	}
	
	public void initialize(){
		Stack stack=new Stack();
		Iterator itr=this.getPage().getRoots().iterator();
		while(itr.hasNext())
			stack.push(itr.next());
		while(!stack.empty()){
			Component comp=(Component)stack.pop();
			mapComponent(comp,stack);
		}
		this.addEventListener("onNotify",new CheckInListener());
		initSelectModal();
	}
	
	private void mapComponent(Component comp,Stack stack){
		String id=comp.getId();
		if(id.equals("CLIENT_NAME"))
			name=comp;
		else if(id.equals("DOCTYPE"))
			doctype=comp;
		else if(id.equals("DOCNUMBER"))
			docNumber=comp;
		else if(id.equals("BIRTHDAY"))
			birthday=comp;
		else if(id.equals("SEX"))
			sex=comp;
		else if(id.equals("REGION"))
			region=comp;
		else if(id.equals("PROVINCE"))
			province=comp;
		else if(id.equals("ADDRESS"))
			address=comp;
		else if(id.equals("NOTE"))
			note=comp;
		else if(id.equals("IS_VIP"))
			isVip=comp;
		else if(id.equals("ADD"))
			add=comp;
		else if(id.equals("CHANGE"))
			change=comp;
		else if(id.equals("DELETE"))
			delete=comp;
		else if(id.equals("COUNT"))
			count=comp;
		else if(id.equals("RECEIVE_DEPOSITE"))
			receiveDeposite=comp;
		else if(id.equals("DEPOSITE"))
			deposite=comp;
		else if(id.equals("PAY_NOTE"))
			payNote=comp;
		else if(id.equals("SELECT"))
			select=comp;
		else if(id.equals("TELPHONE"))
			telPhone=comp;
		else if(id.equals("ROOMTYPE"))
			roomType=comp;
		else if(id.equals("ROOM_NUMBER"))
			roomNumber=comp;
		else if(id.equals("IS_PART"))
			isPart=comp;
		else if(id.equals("TIME_LEAVING"))
			timeLeaving=comp;
		else if(id.equals("ADD"))
			addButton=comp;
		else if(id.equals("DELETE"))
			deleteButton=comp;
		else if(id.equals("UPDATE"))
			updateButton=comp;
		else if(id.equals("COUNT"))
			countButton=comp;
		else if(id.equals("BILL_NUMBER"))
			billNumber=comp;
		else if(comp.getChildren().size() > 0)
		{
			Iterator itr=comp.getChildren().iterator();
			while(itr.hasNext()){
				Component comp=itr.next();
				stack.push(comp);
			}
		}
	}

	public void onSign(){
		billNumberValidate();
		nameValidate();
		telPhoneValidate();
		doctypeValidate();
		docNumberValidate();
		birthdayValidate();
		sexValidate();
		regionValidate();
		provinceValidate();
		addressValidate();
		noteValidate();
		depositeValidate();
		payNoteValidate();
		timeLeavingValidate();
		roomNumberValidate();
		this.getPage().setAttribute("ROOMTYPE",roomType.getSelectedIndex());
		this.getPage().setAttribute("ISPART",isPart.getSelectedIndex());
		this.getPage().setAttribute("VIP",isVip.isChecked());
		this.getPage().setAttribute("RECEIVEDEPOSITE",receiveDeposite.isChecked());
		this.getPage().setAttribute("ACTION","CREATECLIENT");
		Events.sendEvent(new Event("onNotify",this));
	}
	
	//------------------------------------------------------------------------------------------
	//------------------------------------------------------------------------------------------
	//------------------------------------------------------------------------------------------
	//------------------------------------------------------------------------------------------
	//------------------------------------------------------------------------------------------
	//------------------------------------------------------------------------------------------
	//------------------------------------------------------------------------------------------
	//------------------------------------------------------------------------------------------
	
	private Button addBill,updateBill,deleteBill,countBill;
	public void onAdd(){
		billNumberValidate();
		nameValidate();
		telPhoneValidate();
		doctypeValidate();
		docNumberValidate();
		birthdayValidate();
		sexValidate();
		regionValidate();
		provinceValidate();
		addressValidate();
		noteValidate();
		depositeValidate();
		payNoteValidate();
		timeLeavingValidate();
		roomNumberValidate();
		this.getPage().setAttribute("ROOMTYPE",roomType.getSelectedIndex());
		this.getPage().setAttribute("IS_PART",isPart.getSelectedIndex() == 0);
		this.getPage().setAttribute("VIP",isVip.isChecked());
		this.getPage().setAttribute("RECEIVEDEPOSITE",receiveDeposite.isChecked());
		this.getPage().setAttribute("ACTION","DECIDEADD");
		Events.sendEvent(new Event("onNotify",this));
		
		try{
		List data=this.getPage().getAttribute("ROOM_DATA");
		//alert(data.toString());
		if(data == null)
			return;

		for(int i=0;i < data.size();i++){
			List col=new LinkedList();
			List raw=data.get(i);
			col.add(raw.get(0));
			col.add(name.getText());
			col.add(raw.get(1));
			alert(roomType.getSelectedItem()+" "+roomType.getSelectedIndex()+" ");
			col.add(roomType.getSelectedItem().getLabel());
			col.add(isPart.getSelectedIndex() == 0 ? "是" : "否");
			col.add(raw.get(2));
			col.add(timeLeaving.getValue());
			double price=0.0;
			if(isPart.getSelectedIndex() == 0)
				price=((Double)raw.get(2)).doubleValue()*(double)((timeLeaving.getValue().getTime()-new Date().getTime())/3600000);
			else price=((Double)raw.get(2)).doubleValue()*(double)((timeLeaving.getValue().getTime()-new Date().getTime())/86400000);
			
			col.add(price);
			list.insert(col);
		}
		}catch(Exception e){
			try{new Messagebox().show("数据传送失败");}catch(Exception e){}
		}
	}
	
	public void onDelete(){
		try{
			this.getPage().setAttribute("ACTION","DELETE");
			Events.sendEvent(new Event( "onNotify", this ));
			if(this.getPage().getAttribute("IS_DELETED") != null)
				list.del(list.getSelectedIndex());
		}catch(Exception e){
			try{new Messagebox().show("数据传送失败");}catch(Exception e){}
		}
	}
	
	private Window selectWindow;
	private PlainList clientList;
	private void initSelectModal(){
		selectWindow=new Window();
		selectWindow.setMode("overlapped");
		selectWindow.setDraggable("false");
		selectWindow.setClosable(true);
		selectWindow.setParent(this);
		selectWindow.setTitle("选择客人");
		selectWindow.setVisible(false);
		clientList=new PlainList();
		String[] head={"ID","姓名","证件类型","证件号码","生日","性别","区域","省份","地址","电话","备注","VIP"};
		clientList.setHead(head);
		clientList.setParent(selectWindow);
		clientList.setStyle("width:800px");
		
		//initialize button
		Button okBut=new Button("确定");
		okBut.setParent(selectWindow);
		okBut.addEventListener(Events.ON_CLICK,new EventListener(){
			public void onEvent(Event e){
				selectWindow.setVisible(false);
			}
		});
		
		//initialize select action setting
		
		clientList.setListener(new PlainList.SelectListener(){
			public void onFocus(Component comp){
				if(clientList.getSelectedIndex() < 0)
					return;
				List colData=clientList.getColDate(clientList.getSelectedIndex());
			
				name.setValue(((Listcell)colData.get(1)).getLabel());
				
				String type=((Listcell)colData.get(2)).getLabel();
				if(type.equals("身份证"))
					doctype.setSelectedIndex(0);
				else if(type.equals("学生证"))
					doctype.setSelectedIndex(1);
				else if(type.equals("军人证"))
					doctype.setSelectedIndex(2);
				else doctype.setSelectedIndex(3);
			
				docNumber.setValue(((Listcell)colData.get(3)).getLabel());
				Date dat = new Date();
				String dateString = ((Listcell)colData.get(4)).getLabel();
				int firstIndex = dateString.indexOf("-");
				int secondIndex = dateString.indexOf("-", firstIndex+1);
				String year = dateString.substring(0, firstIndex);
				String month = dateString.substring(firstIndex+1,secondIndex);
				String day = dateString.substring(secondIndex+1,dateString.length());
				dat.setYear(Integer.parseInt(year));
				dat.setMonth(Integer.parseInt(month)-1);
				dat.setDate(Integer.parseInt(day));
				birthday.setValue(dat);
			
				type=((Listcell)colData.get(5)).getLabel();
				if(type.equals("男"))
					sex.setSelectedIndex(0);
				else if(type.equals("女"))
					sex.setSelectedIndex(1);
				else sex.setSelectedIndex(2);
			
				region.setValue(((Listcell)colData.get(6)).getLabel());
				province.setValue(((Listcell)colData.get(7)).getLabel());
				address.setValue(((Listcell)colData.get(8)).getLabel());
				telPhone.setValue(new Long(((Listcell)colData.get(9)).getLabel()));
				note.setValue(((Listcell)colData.get(10)).getLabel());
			}
		});
	}
	
	public void onSelectClient(){
		try{
			this.getPage().setAttribute("ACTION","SELECT");
			Events.sendEvent(new Event( "onNotify", this ));
			selectWindow.setVisible(true);
			clientList.update(this.getPage().getAttribute("CLIENT_INFO"));
			selectWindow.setVisible(true);
		}catch(Exception e){}
	}
	
	//------------------------------------------------------------------------------------------
	//------------------------------------------------------------------------------------------
	//------------------------------------------------------------------------------------------
	//------------------------------------------------------------------------------------------
	//------------------------------------------------------------------------------------------
	//------------------------------------------------------------------------------------------
	//------------------------------------------------------------------------------------------
	//------------------------------------------------------------------------------------------
}